#import "QuizController.h"
#import "WordMO.h"

@implementation QuizController

- (IBAction)startNewQuiz:(id)sender
{
	//get all the words currently in the wordArrayController and plop then in our quiz controller
	[gamesTabView selectTabViewItemWithIdentifier:@"BoringFlashQuiz"];
	[self addObjects:[wordArrayController arrangedObjects]];
	[self setSelectionIndex:0];
	[mainWindowTabView selectTabViewItemWithIdentifier:@"GameTabs"];
	NSString *groupName = [[[groupArrayController selectedObjects] objectAtIndex:0] valueForKey:@"name"];
	[quizTitle setStringValue:[NSString stringWithFormat:@"Now Quizzing Words in %@", groupName]];
	[mainWindow setTitle:@"Words Quiz"];
	[quizProgressString setStringValue:[self progressString]];
	[foreignString setHidden:YES];
	[localString setHidden:YES];
	[revealMainDefButton setHidden:NO];
	[revealSecondaryDefButton setHidden:NO];

}

- (IBAction)killQuiz:(id)sender{
	//[wordArrayController addObjects:[self arrangedObjects]]; //clear the array in prep for a new quiz.
	[mainWindow setTitle:@"Words"];
	[mainWindowTabView selectTabViewItemWithIdentifier:@"MainView"];
	[revealMainDefButton setHidden:NO];
	[revealSecondaryDefButton setHidden:NO];
}

- (IBAction)revealForeignMeaning:(id)sender
{
	[foreignString setHidden:NO];
	[sender setHidden:YES];
}



- (IBAction)revealLocalMeaning:(id)sender
{
	[localString setHidden:NO];
	[sender setHidden:YES];

}


- (IBAction)right:(id)sender
{
	WordMO *correctWord = [[self selectedObjects] objectAtIndex:0];
	[correctWord setValue:[NSNumber numberWithBool:YES] forKey:@"known"];
	[correctWord setValue:[NSNumber numberWithBool:NO] forKey:@"maybeKnown"];
	[correctWord setValue:[NSDate date] forKey:@"lastQuizzed"];
	[foreignString setHidden:YES];
	[localString setHidden:YES];
	[revealMainDefButton setHidden:NO];
	[revealSecondaryDefButton setHidden:NO];
	[self selectNext:sender];
}

- (IBAction)almost:(id)sender
{
	WordMO *word = [[self selectedObjects] objectAtIndex:0];
	[word setValue:[NSNumber numberWithBool:YES] forKey:@"maybeKnown"];
	[word setValue:[NSNumber numberWithBool:NO] forKey:@"known"];
	[word setValue:[NSDate date] forKey:@"lastQuizzed"];
	[foreignString setHidden:YES];
	[localString setHidden:YES];
	[self selectNext:sender];
		
}

- (IBAction)wrong:(id)sender
{
	WordMO *wrongWord = [[self selectedObjects] objectAtIndex:0];
	[wrongWord setValue:[NSNumber numberWithBool:NO] forKey:@"known"];
	[wrongWord setValue:[NSNumber numberWithBool:NO] forKey:@"maybeKnown"];
	[wrongWord setValue:[NSDate date] forKey:@"lastQuizzed"];
	[foreignString setHidden:YES];
	[localString setHidden:YES];
	[self selectNext:sender];
}

- (IBAction)skipWord:(id)sender
{
	[foreignString setHidden:YES];
	[localString setHidden:YES];
	[self selectNext:sender];
}

-(void)updateProgressString{
	[quizProgressString setStringValue:[self progressString]];
}

-(void)selectNext:(id)sender{
	//if this is the last word, then we've finished this quiz, so display the results or something.
	int quizLength = [[self arrangedObjects] count];
	int currentIndex = [self selectionIndex];
	currentIndex++; //remove zero indexing
	[revealMainDefButton setHidden:NO];
	[revealSecondaryDefButton setHidden:NO];
	if(quizLength == currentIndex){
		//then we're at the last word! //give me some statistics!
		NSBeep(); //just beep for now
		[self killQuiz:sender];
	}
	else{
		[super selectNext:sender];
		//[self updateProgressString];
	}
}

- (NSString *)progressString{
	int quizLength = [[self arrangedObjects] count];
	int currentIndex = [self selectionIndex];
	currentIndex++; //remove zero indexing
	NSString *progress = [NSString stringWithFormat:@"Word %d of %d", currentIndex, quizLength];
	return progress;
}





@end
